pipeline {
    agent any

    environment {
        // 镜像版本
        IMAGETAG="latest"
        // Harbor私服地址
        HARBOR_URL="192.168.88.131:85"
        // Harbor的项目名称
        HARBOR_PROJECT="demo1"
        PORT=8581
    }


    stages {
        stage('checkout SCM') {
            steps {
                checkout scmGit(branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: '1', url: 'https://gitee.com/yyyyjinying/free-marker-project.git']])
            }
        }
        stage('maven打包') {
            steps {
                sh '/export/server/maven/bin/mvn clean package -Dmaven.test.skip=true'
                echo "maven打包构建成功"
            }
        }
        stage('docker镜像生成') {
            steps {
                script {
                    def remote = [:]
                    remote.name = 'node1'
                    remote.host = '192.168.88.131'
                    remote.allowAnyHosts = true
                    withCredentials([sshUserPrivateKey(credentialsId: '12', keyFileVariable: 'identity', passphraseVariable: 'sh123456', usernameVariable: 'userName')]) {
                        remote.user = userName
                        remote.identityFile = identity

                        // 执行远程脚本jar-start.sh，传递一个参数arg1 PORT实际暴漏的端口号可以自由设置
                        def commandWithArgs = "/export/server/docker-start/base/sores/jenkins/jenkins-data/workspace/${env.JOB_NAME}/build.sh ${env.HARBOR_URL} ${env.HARBOR_PROJECT} ${PROJECTNAME} ${env.IMAGETAG}"
                        sshCommand remote: remote, command: commandWithArgs
                    }
                }
            }
        }
        stage('Deploy') {
            steps {
                script {
                    def remote = [:]
                    remote.name = 'node1'
                    remote.host = '192.168.88.131'
                    remote.allowAnyHosts = true
                    withCredentials([sshUserPrivateKey(credentialsId: '12', keyFileVariable: 'identity', passphraseVariable: 'sh123456', usernameVariable: 'userName')]) {
                        remote.user = userName
                        remote.identityFile = identity

                        // 执行远程脚本jar-start.sh，传递一个参数arg1 PORT实际暴漏的端口号可以自由设置
                        def commandWithArgs = "/export/server/docker-start/base/sores/jenkins/execssh/image_deploy.sh ${env.HARBOR_URL} ${env.HARBOR_PROJECT} ${PROJECTNAME} ${env.IMAGETAG} ${env.PORT}"
                        sshCommand remote: remote, command: commandWithArgs
                    }
                }

                 echo 'build project success ${ANOTHER_VAR}'
            }
        }
    }
}
